package co.yixiang.yshop.module.product.controller.app.couponrelation;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.product.controller.app.couponrelation.vo.UserCouponVO;
import co.yixiang.yshop.module.product.service.productcouponrelation.AppCouponRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 商品优惠券关联")
@RestController
@RequestMapping("/product/coupon/relation")
@Validated
public class AppCouponRelationController {

    @Resource
    private AppCouponRelationService appCouponRecordService;


    @GetMapping("/receive/{id}")
    @Operation(summary = "领取优惠券")
    @Parameter(name = "id", description = "优惠券id", required = true, example = "1")
    @PreAuthenticated
    public CommonResult<Boolean> receiveCoupon(@PathVariable Long id) {
        return success(appCouponRecordService.receiveCoupon(id, getLoginUserId()));
    }

    @GetMapping("/receive/cdkey/{code}")
    @Operation(summary = "通过兑换码领取优惠券")
    @Parameter(name = "code", description = "优惠券兑换码", required = true, example = "1")
    @PreAuthenticated
    public CommonResult<Boolean> receiveCouponByCode(@PathVariable String code) {

        return success(appCouponRecordService.receiveCouponByCode(code, getLoginUserId()));
    }

    @GetMapping("/searchUserCoupon/{type}")
    @Operation(summary = "查询优惠券")
    @Parameter(name = "type", description = "查询类型：1、可使用，2、已使用，3、已失效", required = true, example = "1")
    @PreAuthenticated
    public CommonResult<List<UserCouponVO>> searchUserCoupon(@PathVariable Integer type) {
        return success(appCouponRecordService.searchUserCoupon(type,getLoginUserId(), null));
    }

}
